﻿Imports System.Diagnostics.Process
Imports System.Reflection
Imports System.Windows.Forms

Module modProcessos
    Public Function VerificaInstancia(Optional ByVal parMsg As Boolean = True) As Boolean
        Try
            Dim processoAtual As Process = Process.GetCurrentProcess()
            Dim processosRodando As Process() = Process.GetProcessesByName(processoAtual.ProcessName)
            Dim processo As Process
            For Each processo In processosRodando
                If processo.Id <> processoAtual.Id Then
                    If Assembly.GetExecutingAssembly().Location.Replace("/", "\") = processoAtual.MainModule.FileName Then
                        If parMsg Then MessageBox.Show("O sistema já está aberto.")
                        Return True
                    End If
                End If
            Next processo
            Return False
        Catch ex As Exception
            GravaErro(Err, ex, , , "VerificaInstancia")
            Return False
        End Try
    End Function
    Public Function existeProcessoAbertoLocal(ByVal processo As String)
        Try
            Dim processos = GetProcessesByName(processo)
            If processos.GetLength(0) > 0 Then
                Return True
            End If
        Catch ex As Exception
            Return False
        End Try
        Return False
    End Function
    Public Function existeInstanciaPrograma(ByVal processo As String)
        Try
            Dim processos = GetProcessesByName(processo)
            If processos.GetLength(0) > 1 Then
                Return True
            End If
        Catch ex As Exception
            Return False
        End Try
        Return False
    End Function

    Public Function existeProcessoAbertoRede(ByVal processo As String, ByVal maquina As String)
        Try
            Dim processos = GetProcessesByName(processo, maquina)
            If processos.GetLength(0) > 0 Then
                Return True
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return False
    End Function

    '    Private Const TH32CS_SNAPPROCESS As Long = 2&


    '    Private Structure PROCESSENTRY32
    '        Dim dwSize As Long
    '        Dim cntUsage As Long
    '        Dim th32ProcessID As Long
    '        Dim th32DefaultHeapID As Long
    '        Dim th32ModuleID As Long
    '        Dim cntThreads As Long
    '        Dim th32ParentProcessID As Long
    '        Dim pcPriClassBase As Long
    '        Dim dwFlags As Long
    '        Dim szExeFile As String
    '    End Structure


    '    Private Const PROCESS_ALL_ACCESS As Long = &H1F0FFF
    '    Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
    '    Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, ByVal uProcess As PROCESSENTRY32) As Long
    '    Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, ByVal uProcess As PROCESSENTRY32) As Long
    '    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    '    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    '    Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)

    '    Public Function LocalizaProcesso(parEXEName As String, Optional parFinaliza As Boolean) As Boolean
    '        Dim hSnapShot As Long
    '        Dim uProcess As PROCESSENTRY32
    '        Dim hProcess As Long
    '        hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
    '        If hSnapShot = -1 Then Exit Function
    '        uProcess.dwSize = Len(uProcess)
    '        If ProcessFirst(hSnapShot, uProcess) = 1 Then
    '            Do
    '                If LCase$(Left$(uProcess.szExeFile, InStr(1, uProcess.szExeFile, vbNullChar) - 1)) = LCase$(parEXEName) Then
    '                    If parFinaliza Then
    '                        hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
    '               ' LocalizaProcesso = TerminateProcess(hProcess, ByVal 0&) > 0
    '                        If Not LocalizaProcesso Then Exit Function
    '                    Else
    '                        LocalizaProcesso = True
    '                        Exit Do
    '                    End If
    '                End If
    '            Loop While ProcessNext(hSnapShot, uProcess)
    '        End If
    '    End Function

    '    Public Function localizaProcesso()

    '        Return False

    '    End Function
End Module
